package createThread;

/**
 * todo
 *
 * @author: luolidong
 * @create: 2021-07-08 16:36
 * @version: 1.0
 */
public class SyncTest {
    public final Object object = new Object();
    public final Object object1 = new Object();

    /*
    public void get() throws InterruptedException {
        synchronized (this){
            System.out.println("get");
            Thread.sleep(1000);
        }
    }
    public void set(){
        synchronized (object1){
            System.out.println("set");
        }
    }
    */
    public synchronized void get() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("get");
    }
    //方法锁只锁调用该方法的对象
    public synchronized void set() {
        System.out.println("set");
    }

    public static void main(String[] args) {
        SyncTest syncTest = new SyncTest();
        SyncTest syncTest2 = new SyncTest();
        new Thread(() -> {
            try {
                syncTest.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            syncTest2.set();
        }).start();
    }
}
